// 测试AES加密解密字符串的代码
const crypto = require('crypto');

// AES 加密
function encrypt(text, secretKey) {
  const iv = crypto.randomBytes(16); // 初始化向量
  const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(secretKey), iv);
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted += cipher.final('hex');
  return iv.toString('hex') + ':' + encrypted;
}

// AES 解密
function decrypt(encryptedText, secretKey) {
  const [ivHex, encrypted] = encryptedText.split(':');
  const iv = Buffer.from(ivHex, 'hex');
  const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(secretKey), iv);
  let decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}

const secretKey = 'yilongchen1yilongchen2yilongchen'; // 必须是 32 字符
const originalText = 'zhangsan*30*1735277645985';

const encrypted = encrypt(originalText, secretKey);
console.log('Encrypted:', encrypted);

const decrypted = decrypt(encrypted, secretKey);
console.log('Decrypted:', decrypted);